package com.example.myviewmodelframe

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.myviewmodelframe.base.activity.BaseActivity
import com.example.myviewmodelframe.base.model.BaseViewModelFactory
import com.example.myviewmodelframe.base.model.BaseViewModelParamFactory
import com.example.myviewmodelframe.databinding.ActivityMainBinding
import com.example.myviewmodelframe.model.MainActivityViewModel
import java.util.concurrent.*


class MainActivity : BaseActivity<ActivityMainBinding, MainActivityViewModel>(),
    View.OnClickListener {
    lateinit var thread: ScheduledExecutorService

    private lateinit var activityMainModel: MainActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind.btLoad.setOnClickListener(this)
        thread = Executors.newScheduledThreadPool(1)

        activityMainModel.getLiveData()?.observe(this, {
            Log.d("tgw", "getViewModel: " + it.toString())
            mBind.btLoad.text = it.name
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            mBind.btLoad -> {
                activityMainModel.startLoading("消息看看")
                var h = Handler(Looper.myLooper()!!)
                h.postDelayed(Runnable {
                    activityMainModel.dismissLoading()
                    activityMainModel.createButtonText()
                }, 5000)
//                thread.schedule(Runnable {
//                    baseViewModel.dismissLoading()
//                }, 5, TimeUnit.SECONDS)
            }
        }

    }

    override fun getViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun getViewModel(): MainActivityViewModel {

//        activityMainModel = BaseViewModelFactory().create(MainActivityViewModel::class.java)
//        activityMainModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
        activityMainModel = BaseViewModelParamFactory(this.application,"null").create(MainActivityViewModel::class.java)
        /**生命周期*/
        activityMainModel.lifecycleOwner = this
        return activityMainModel as MainActivityViewModel
    }
}